﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FitnessDiary.WebUI.Models;
using FitnessDiary.Domain.Entities;
using System.Text;

namespace FitnessDiary.WebUI.ViewHelpers
{
	public static class ChartHelper
	{
		public static MvcHtmlString DataToChart(this HtmlHelper html, ChartModel model)
		{
			ChartData chartData = CreateChartData(model);
			if (chartData.ChartDays.Count < 1)
			{
				return new MvcHtmlString("");
			}
			StringBuilder result = new StringBuilder();
			foreach (ChartDay chartDay in chartData.ChartDays)
			{
				result.Append("{ \"Date\": \"" + chartDay.Date.ToString("yyyy.MM.dd.") + "\", ");
				foreach (HitComponent hitComponent in chartDay.Hit.HitComponents)
				{
					result.Append("\"" + hitComponent.Quantity.Name + "\": " + hitComponent.Amount + ",");
				}
				result.Remove(result.Length - 1, 1);
				result.Append(" },\n");
			}
			result.Remove(result.Length - 2, 2);
			return MvcHtmlString.Create(result.ToString());
		}

		public static ChartData CreateChartData(ChartModel model)
		{
			ChartData chartData = new ChartData();
			chartData.ChartDays = new List<ChartDay>();
			TimeSpan ts = model.EndDate - model.BeginDate;
			int numberOfDays = ts.Days;
			for (int i = 0; i <= numberOfDays; i++)
			{
				DateTime actualDay = model.BeginDate.AddDays(i);
				ChartDay chartDay = new ChartDay { Date = actualDay };
				chartDay.Hit = CreateChartDayHit(model.Hits.Where(h => h.Date == actualDay).ToList());
				chartData.ChartDays.Add(chartDay);
			}
			return chartData;
		}

		public static Hit CreateChartDayHit(List<Hit> hits)
		{
			if (hits.Count > 0)
				return hits.First();
			else
				return new Hit { HitComponents = new List<HitComponent>() };
		}

		public static MvcHtmlString SetSeries(this HtmlHelper html, int numberOfSeries)
		{
			StringBuilder result = new StringBuilder();
			for (int i = 0; i < numberOfSeries; i++)
			{
				result.Append(String.Format("var series{0} = chart.getSeries().getItem({1});", i + 1, i));
			}
			return new MvcHtmlString(result.ToString());
		}

		public static MvcHtmlString SetGallery(this HtmlHelper html, int numberOfSeries)
		{
			StringBuilder result = new StringBuilder();
			for (int i = 0; i < numberOfSeries; i++)
			{
				result.Append(String.Format("series{0}.setGallery(cfx.Gallery.Bar);", i + 1));
			}
			return new MvcHtmlString(result.ToString());
		}

		public static MvcHtmlString CreateCheckboxes(this HtmlHelper html, Hit hit)
		{
			StringBuilder result = new StringBuilder();
			for (int i = 0; i < hit.HitComponents.Count; i++)
			{
				result.Append(String.Format(hit.HitComponents[i].Quantity.Name + ": <input id=\"Checkbox{0}\" type=\"checkbox\" checked=\"CHECKED\" onclick=\"swap({0}, Checkbox{0});\" />", i + 1));
			}
			return new MvcHtmlString(result.ToString());
		}
	}

	public class ChartData
	{
		public int Max { get; set; }
		public List<ChartDay> ChartDays { get; set; }
	}

	public class ChartDay
	{
		public DateTime Date { get; set; }
		public Hit Hit { get; set; }
	}
}